package com.gxhmplan.cases.web;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxhmplan.cases.entity.Case;
import com.gxhmplan.cases.service.CaseService;
import com.gxhmplan.util.JsonResult;
import com.gxhmplan.util.Util;

@Controller
@RequestMapping("")
public class CaseController {
	@Resource
	CaseService caseService;
	
	@RequestMapping("/case.htm")
	public ModelAndView casePage() {
		return new ModelAndView("/WEB-INF/pages/case/case.jsp");
	}
	
	@RequestMapping("/addCase.htm")
	public ModelAndView addCase(Case c) {
		caseService.addCase(c);
		return new ModelAndView("redirect:/case.htm");
	}
	
	@RequestMapping("/caseMangent.htm")
	public ModelAndView caseMangent(Integer page) {
		if(null==page || "".equals(page)){
			page=1;
		}
		ModelAndView mav = new ModelAndView();
		PageHelper.startPage(page, 15);//分页拦截器  显示条数
		List<Map<String, Object>> caseList = caseService.findCase();
		PageInfo<Map<String, Object>> p = new PageInfo<Map<String, Object>>(caseList);
		mav.addObject("caseList", p.getList());
		mav.addObject("total", p.getTotal());// 总数
		mav.addObject("pages", p.getPages());// 总页数
		mav.addObject("pageNum", p.getPageNum());//页数
		mav.addObject("url", "caseMangent.htm");//路径
		mav.setViewName("/WEB-INF/pages/case/caseMangent.jsp");
		return mav;
	}
	
	@RequestMapping("/deleteCase.htm")
	public ModelAndView deleteCase(String id) {
		caseService.deleteCaseById(id);
		return new ModelAndView("redirect:/caseMangent.htm");
	}
	
	@RequestMapping("/childCase.htm")
	public ModelAndView childcase(String classify,Integer page) {
		if (classify==null||classify.trim().isEmpty()) {
			classify = "1";
		}
		ModelAndView mav = new ModelAndView();
		if(null==page || "".equals(page)){
			page=1;
		}
		PageHelper.startPage(page, 6);//分页拦截器  显示条数
		List<Map<String, Object>> caseList = caseService.findCaseByClassify(classify);
		PageInfo<Map<String, Object>> p = new PageInfo<Map<String, Object>>(caseList);
		mav.addObject("caseList", p.getList());
		mav.addObject("total", p.getTotal());// 总数
		mav.addObject("pages", p.getPages());// 总页数
		mav.addObject("pageNum", p.getPageNum());//页数
		mav.addObject("url", "childCase.htm?classify="+classify);//路径
		mav.addObject("classify", classify);
		mav.setViewName("/WEB-INF/pages/show/childcase.jsp");
		return mav;
	}
	@RequestMapping("/tourism.htm")
	public ModelAndView tourism(String caseNum) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("case1", caseService.findFrontCaseById(caseNum));
		mav.addObject("case2", caseService.findAfterCaseById(caseNum));
		mav.addObject("cases", caseService.findCaseById(caseNum));
		mav.setViewName("/WEB-INF/pages/show/tourism.jsp");
		return mav;
	}
	@ResponseBody
	@RequestMapping("/uploadImg.do")
	public JsonResult uploadImg(MultipartFile file,HttpServletRequest request) {
		return new JsonResult(Util.upload(file, Util.reqGetId(request), "coverImg", request));
	}
}
